import qs from "qs";
// import * as http from '@/utils/sgmw_auth/http'
import { http } from "@/utils/http";
/**
 *获取用户Token
 * @param code 临时授权码
 * @returns token
 */
export function accessToken(code: string) {
  // let requestUri = `${import.meta.env.VITE_APP_OidcUrl}token`;
  let requestUri =
    "https://account-test.sgmw.com.cn/auth/realms/demo/protocol/openid-connect/token";

  let clientid = import.meta.env.VITE_APP_OidcId;
  const grantType = "authorization_code";
  const redirectUri =
    window.location.origin + import.meta.env.VITE_APP_OidcRedirect;
  let data = {
    client_id: clientid,
    code,
    grant_type: grantType,
    redirect_uri: redirectUri
  };
  return http.request<any>("post", requestUri, {
    data,
    headers: {
      "Content-Type": "application/x-www-form-urlencoded"
    }
  });
}

/**
 * 获取用户信息
 * @param token 用户认证token
 * @returns 用户信息
 */
export function getUserInfo(token: string) {
  // let OidcUrl = import.meta.env.VITE_APP_OidcUrl;
  // let url = `/master/oidc/refreshUserToken`;
  let requestUri =
    "https://account-test.sgmw.com.cn/auth/realms/demo/protocol/openid-connect/userinfo";
  let headers = {
    Authorization: `Bearer ${token}` // 将 Bearer Token 放置在 Authorization 头中
  };
  return http.request<any>("post", requestUri, {
    headers
  });
}

/**
 * 获取用户信息
 * @param token 用户认证token
 * @returns 用户信息
 */

// export function getUserProfile(token: string) {
//   let OidcUrl = import.meta.env.VITE_APP_OidcUrl;
//   let url = `/master/oidc/RefreshProfileToken`;
//   return http.request("post", OidcUrl, {
//     url,
//     headers: {
//       "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
//     }
//   });
// }
/**
 * 用户授权认证登出
 * @param token 用户刷新token
 * @returns 请求成功返回状态码204
 */
export function logout(token: string) {
  console.log("退出成功啦~"); // let url = `${import.meta.env.VITE_APP_OidcUrl}logout`;
  let requestUri =
    "https://account-test.sgmw.com.cn/auth/realms/demo/protocol/openid-connect/logout";
  let data = {
    refresh_token: token,
    client_id: import.meta.env.VITE_APP_OidcId
  };
  let headers = {
    "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
  };
  return http.request<any>("post", requestUri, {
    headers: headers,
    data: data
  });
}
